package com.ruoyi.web.controller.system;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.system.domain.Que;
import com.ruoyi.system.domain.Ques;
import com.ruoyi.system.domain.vo.Results;
import com.ruoyi.web.controller.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api")
public class questionSubController {
    @Autowired
    private com.ruoyi.system.service.questionSubService questionSubService;
    @Autowired
    private com.ruoyi.system.mapper.questionSubMapper questionSubMapper;

    @PostMapping("/save")
    @CrossOrigin
    public R<String> save(@RequestBody Que que){
        //mybatisplus自带的插入语句
        questionSubService.save(que);
        return R.success("新增成功");
    }

    //分页查询
    @GetMapping("/page/{page}/{pageSize}")
    public R<Page> pageR(@PathVariable("page") int page,@PathVariable("pageSize") int pageSize){
        //分页构造器
        Page<Que> pageInfo = new Page<>(page,pageSize);
        //条件构造器
        LambdaQueryWrapper<Que> queryWrapper = new LambdaQueryWrapper<>();
        //添加排序条件，根据id进行排序
        queryWrapper.orderByAsc(Que::getQueId);
        //进行分页查询
        questionSubService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }
    //根据条件查询（选择/填空）
    //不需要分页，因为数据回显后会再次调用分页查询
    @GetMapping("/selectByType")
    public R<List<Que>> selectByType(@RequestBody Que que){
        //条件构造器
        LambdaQueryWrapper<Que> queryWrapper = new LambdaQueryWrapper<>();
        //获取属性类型
        queryWrapper.eq(Que::getQueType,que.getQueType());
        //查询
        queryWrapper.orderByDesc(Que::getQueId);
        //将数据存入list
        List<Que> list = questionSubService.list(queryWrapper);

        return R.success(list);
    }
    //根据条件查询（前端/后端/全栈）
    @PostMapping("/selectByFincation")
    public R<List<Que>> selectByFincation(@RequestBody  Que que){
        //条件构造器
        LambdaQueryWrapper<Que> queryWrapper = new LambdaQueryWrapper<>();
        //第一个参数是数据库所查的数据名，第二个是数据值
            queryWrapper.like(Que::getQueName, que.getQueName());
            //查询
            queryWrapper.orderByDesc(Que::getQueId);
            //将数据存入list
            List<Que> list = questionSubService.list(queryWrapper);
        return R.success(list);
    }


    //删除
    @DeleteMapping("/deleteQue/{id}")
    public R<String> delete(@PathVariable("id") int id){
        questionSubService.removeById(id);
        return  R.success("删除成功");
    }

    //根据id查询题目
    @GetMapping("/selectById/{id}")
    public R<Que> selectById(@PathVariable("id") Integer id){
        Que que =questionSubService.getById(id);
        return R.success(que);
     }

    //根据userid与id为空查询初始试题
    @PostMapping("/selectByUserId")
    public R<List<Que>> selectByUserId(@RequestBody Que que){
        //条件构造器
        LambdaQueryWrapper<Que> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(Que::getUserId, que.getUserId());
            queryWrapper.orderByAsc(Que::getCreateTime);
            List<Que> list = questionSubService.list(queryWrapper);
            return R.success(list);
    }
    //根据搜索框与类型查询
    @GetMapping("/selectByIndex/{fication}/{str}")
    public R<List<Que>> selectByIndex(@PathVariable("fication") int fication,@PathVariable("str") String str) {
        LambdaQueryWrapper<Que> queryWrapper = new LambdaQueryWrapper<>();
            //根据fincation 值与搜索栏中传入的值搜索
            queryWrapper.like(Que::getQueName, str)
                    .eq(Que::getQueFication,fication);
            queryWrapper.orderByAsc(Que::getCreateTime);
            List<Que> list = questionSubService.list(queryWrapper);
            return  R.success(list);
    }
    @GetMapping("/selectBy/{fication}")
    public R<List<Que>> selectByFication(@PathVariable("fication") Integer fication) {
        LambdaQueryWrapper<Que> queryWrapper = new LambdaQueryWrapper<>();
        //根据fincation 值与搜索栏中传入的值搜索
        queryWrapper.eq(Que::getQueFication,fication);
        queryWrapper.orderByAsc(Que::getCreateTime);
        List<Que> list = questionSubService.list(queryWrapper);
        return  R.success(list);
    }
    //修改试题
    @PostMapping("/update/St")
    public Results updateSt(@RequestBody Que que){
//        LambdaQueryWrapper<Que> queryWrapper = new LambdaQueryWrapper<>();
//        queryWrapper.eq(Que::getQueId,que.getQueId());
        boolean b = questionSubService.updateById(que);
        if(b){
            return new Results(400,"更改成功","");
        }
        return new Results(500,"更改失败","");
    }
}

